package OrderedCoverPackage;

import ClassifiedDataSetPackage.ClassifiedDataSet;
import HierarchyPackage.HClass;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author Martin Haulrich
 */
public class SC {

    Map<HClass, Set<Integer>> classifications;

    List<HClass> sortedClassifications;

    boolean sorted;

    final classificationComparator comp;

    final int mincount;

    public SC(ClassifiedDataSet cd, int mincount) {
        classifications = new HashMap<HClass, Set<Integer>>(cd.classCount());
        sortedClassifications = new ArrayList<HClass>(cd.classCount());
        for (HClass c : cd.classifications()) {
            sortedClassifications.add(c);
//            classifications.put(c, new HashSet<Integer>(cd.getInstances(c)));
            classifications.put(c, cd.getInstances(c));
        }
        comp = new classificationComparator();
        
        sorted = false;
        this.mincount = mincount;
        tempRemoved = new HashSet<HClass>();
    }

    public List<HClass> getNBest(int N) {
       

        if (!sorted) {
            Collections.sort(sortedClassifications, comp);
        }
        return sortedClassifications.subList(0, Math.min(N, sortedClassifications.size()));
    }

    public void removeTemp(HClass c) {
//        System.err.println("removing: " + c);
        sortedClassifications.remove(c);
        tempRemoved.add(c);

    }

    public void reset() {
        sortedClassifications.addAll(tempRemoved);
//        System.err.println("reint: " + tempRemoved);
        tempRemoved.clear();
    }
Collection<HClass> tempRemoved;

   

    public void removeTrue(HClass c) {
//        System.err.println("TRUE: " + c);
        classifications.remove(c);
        sortedClassifications.remove(c);
        tempRemoved.remove(c);
        sorted = false;
    }

    public void remove(Set<Integer> data) {
        for (Iterator<HClass> it = classifications.keySet().iterator(); it.hasNext(); ) {


//        for (Iterator<Collection<Integer>> it = classifications.values().iterator(); it.hasNext();) {
            HClass c = it.next();
            Collection<Integer> myData = classifications.get(c);
            
            myData.removeAll(data);
            if (myData.size() < mincount) {
                sortedClassifications.remove(c);
                tempRemoved.remove(c);
                it.remove();
                
            }
        }
        sorted = false;
    }

    public boolean isEmpty() {
        return sortedClassifications.isEmpty();
    }

    public Set<Integer> getData(HClass c) {
        return classifications.get(c);
    }

    class classificationComparator implements Comparator<HClass> {

        @Override
        public final int compare(HClass a, HClass b) {
        
            Integer ca = classifications.get(a).size();
            Integer cb = classifications.get(b).size();

            if (ca.equals(cb)) {
                return 1;
            } else {
                return cb.compareTo(ca);
            }
        }
    }
}
